<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="会员名称">
                <a-input placeholder="请输入会员名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="电话号">
                <a-input placeholder="请输入电话号" v-model="queryParam.mobile"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="0"> 待审核 </a-radio-button>
          <a-radio-button value="1"> 已审核 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: true }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.avatar" alt="" />
            </div>
            <div class="info">
              <span>{{ record.nickname }}</span>
            </div>
          </div>
        </div>
        <span slot="mobile" slot-scope="scope"> {{ scope.name }} / {{ scope.mobile }} </span>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="audit_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="status" slot-scope="text">
          {{ text == 0 ? '待审核' : text == 1 ? '已通过' : '已拒绝' }}
        </span>
        <div slot="action" slot-scope="text, scope">
          <div class="action">
            <a-button type="link" @click="auditShareholder(scope.id, 1)" size="small" :disabled="scope.status != 0"
              >通过</a-button
            >
            <span class="line"></span>
            <a-button type="link" @click="auditShareholder(scope.id, 2)" size="small" :disabled="scope.status != 0"
              >拒绝</a-button
            >
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { auditList, audit, allLevel } from '@/api/shareholder'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '会员信息',
    scopedSlots: {
      customRender: 'name',
    },
    width: 200,
  },
  {
    title: '姓名/电话号',
    scopedSlots: {
      customRender: 'mobile',
    },
    width: 200,
  },
  {
    title: '地区',
    dataIndex: 'address',
    width: 200,
  },
  {
    title: '申请时间',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
    sorter: (a, b) => a.created_at - b.created_at,
    width: 200,
  },
  {
    title: '审核时间',
    dataIndex: 'audit_time',
    scopedSlots: {
      customRender: 'audit_time',
    },
    sorter: (a, b) => a.audit_time - b.audit_time,
    width: 200,
  },
  {
    title: '状态',
    width: 100,
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 120,
    dataIndex: 'action',
    scopedSlots: {
      customRender: 'action',
    },
  },
]
export default {
  data() {
    return {
      // 搜索参数
      queryParam: {
        name: null,
        mobile: null,
        status: '0',
        pageNo: 1,
        pageSize: 10,
      },
      // 请求的数据列表
      list: [],
      // 表格列配置
      columns,
      // 数据条数
      totalCount: 0,
      // 所有等级列表
      levelList: [],
      // 选择等级的弹窗是否显示
      showLevel: false,
      // 编辑等级的表单
      levelForm: {
        id: null,
        level_id: null,
      },
    }
  },
  methods: {
    // 获取数据列表
    async getList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await auditList(this.queryParam)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.list = res.data.data
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 审核股东
    auditShareholder(id, status) {
      let that = this
      let text = status == 1 ? '同意' : '拒绝'
      this.$confirm({
        title: '是否' + text + '申请?',
        onOk() {
          that.audit([id], status)
        },
        onCancel() {},
      })
    },
    // 审核
    async audit(id, status, level_id) {
      const res = await audit({
        id,
        status,
        level_id,
      })
      if (res.code == 0) {
        this.closeEdit()
        this.getList()
      }
    },
    // 获取所有等级列表
    async allLevel() {
      const res = await allLevel()
      if (res.code == 0) {
        this.levelList = res.data
      }
    },
    // 关闭编辑等级弹框重置表单数据
    closeEdit() {
      this.levelForm = {
        level_id: null,
      }
      this.showEdit = false
    },
  },
  mounted() {
    this.getList()
    this.allLevel()
  },
}
</script>

<style lang="less" scoped>
.search {
  /deep/ .ant-card-body {
    padding-top: 0 !important;
    padding-bottom: 14px !important;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .yellew {
    background-color: #faad14;
  }
  .refuse {
    background-color: #f5222d;
  }
  .aggree {
    background-color: #65c817;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
</style>